package main

import (
	"fmt"

	"github.com/spf13/cobra"
)

/*
go run main.go user --help
go run main.go user --page=1
go run main.go user --page 1
go run main.go user -p=2 -s=3
*/
func main() {

	var page int32
	var pageSize int32

	fmt.Println("start")

	var rootCmd = &cobra.Command{
		Use:   "root",
		Short: "A short description of your application",
		Long:  "A longer description of your application",
		Run: func(cmd *cobra.Command, args []string) {
			// 定义根命令的执行逻辑
			fmt.Println("=== Root command executed")
		},
	}

	var userCmd = &cobra.Command{
		Use:   "user",
		Short: "A short description of the subcommand",
		PreRun: func(cmd *cobra.Command, args []string) {
			fmt.Printf("=== user start args:%v page:%v \n", args, page)
		},
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf("=== user search args:%v page:%v pageSize:%v \n", args, page, pageSize)
		},
		PostRun: func(cmd *cobra.Command, args []string) {
			fmt.Printf("=== user end \n")
		},
	}

	userCmd.Flags().Int32VarP(&page, "page", "p", 0, "第几页")
	userCmd.Flags().Int32VarP(&pageSize, "pageSize", "s", 10, "一页多少条")
	userCmd.MarkFlagRequired("page")

	// 还可以继续add其他的
	rootCmd.AddCommand(userCmd)
	err := rootCmd.Execute()
	fmt.Printf("Execute() err:%v \n", err)

	fmt.Println("end")

}
